#!/usr/bin/python2.4
#
#Copyright 2009 Google Inc.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#

"""This module provides handler to handle create new version request.

Creating a new version is done in various steps. This modle provides
CreateVersionHandler that controls the order in which these steps are executed.
"""

import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import base_request_handler
import constants
import db_utils


class CreateVersionHandler(base_request_handler.BaseRequestHandler):
  """Request handler for creating new version requests.

  Creating a new version is done in following steps:
    1. Get Tab separated file containing metadata.
    2. Get Tab separated file containing globaldata.
    3. Choose content type PDF/Video. for PDF go to step 4. For video, go to
    step 5.
    4. If content type is PDF, one-by-one upload should happen. When this
    upload is finished this step is completed.
    5. Upload images.
    6. generate website.
  This handler maintains the status of the create version process and executes
  the next step everytime it is called.
  """

  def _Handler(self, status):
    """GET and POST handler."""
    db_handler = db_utils.DbHandler()
    logging.info('status = ' + status)
    db_handler.UpdateStatus(status)

    if status == constants.GET_METADATA:
      message = 'Enter the path to Tab Separated file for Metadata:'
      link = '/getmetadata'
      version_id = db_handler.CreateNewVersion()
      logging.debug('New Version Id created ')
      logging.debug(version_id)
      template_values = {
          'link': link,
          'message': message,
          }
      self.response.out.write(self.GenerateHtml(
          'gettrix.html',
          template_values))
    elif status == constants.VERIFY_METADATA:
      template_values = {}
      template_values['redirect_url'] = '/updatemetadata'
      self.response.out.write(
          self.GenerateHtml('redirection.html',
                            template_values))
    elif status == constants.GET_GLOBALDATA:
      message = 'Enter the path to Tab Separated file for  Global data:'
      link = '/getglobal'
      template_values = {'link': link, 'message': message}
      version_id = db_handler.GetLatestVersion()
      self.response.out.write(self.GenerateHtml('gettrix.html',
                                                template_values))
    elif status == constants.IMAGE_UPLOAD:
      template_values = {}
      skip_image_url = '/create?status=' + constants.GENERATE_WEBSITE
      template_values['skip_image_url'] = skip_image_url
      self.response.out.write(self.GenerateHtml('image_upload.html',
                                                template_values))
    elif status == constants.GENERATE_WEBSITE:
      self.response.out.write(self.GenerateHtml('generate_website.html',
                                                {}))
    else:
      logging.error('invalid status ' + status)
      self.redirect('/admin')

  def get(self):
    """GET method for this request handler."""
    status = self.request.get('status')
    self._Handler(status)

  def post(self):
    """POST method for this request handler."""
    status = self.request.get('status')
    self._Handler(status)


application = webapp.WSGIApplication(
    [('/create', CreateVersionHandler)],
    debug=True)


def main():
  run_wsgi_app(application)


if __name__ == '__main__':
  main()
